$().ready(function () {
    validateRule();
});

$.validator.setDefaults({
    submitHandler: function () {
        save();
    }
});

function save() {
    countCategoryLines();

    $.ajax({
        cache: true,
        type: "POST",
        url: "/usabb/maternityCombos/save",
        data: $('#signupForm').serialize(),// 你的formid
        async: false,
        error: function (request) {
            parent.layer.alert("Connection error");
        },
        success: function (data) {
            if (data.code == 0) {
                parent.layer.msg("操作成功");
                parent.reLoad();
                var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                parent.layer.close(index);

            } else {
                parent.layer.alert(data.msg)
            }
        }
    });

}

function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#signupForm").validate({
        rules: {
            name: {
                required: true
            }
        },
        messages: {
            name: {
                required: icon + "请输入姓名"
            }
        }
    })
}

//服务条目计数器
var cateCounter = 1;

//新增页面元素
function createCategory() {
    var cateElement = "<div class=\"cateId_" + (cateCounter + 1) +"\">\n" +
        "                            <div class=\"form-group\">\n" +
        "                                <label class=\"col-sm-3 control-label\">服务类目：</label>\n" +
        "                                <div class=\"col-sm-3\">\n" +
        "                                    <input name=\"category\" class=\"form-control\" type=\"text\"> \n" +
        "                                </div>\n" +

        "                                <div class=\"col-sm-3 col-sm-offset-3\">\n" +
        "                                    <!--<button type=\"button\" class=\"btn btn-primary\" onclick=\"createCategory()\">新增</button>-->\n" +
        "                                </div>\n" +
        "                            </div>\n" +
        "                            <div class=\"form-group\">\n" +
        "                                <label class=\"col-sm-3 control-label\">服务类目内容：</label>\n" +
        "                                <div class=\"col-sm-8\">\n" +
        "                                    <div class=\"row\">\n" +
        "                                        <div class=\"col-sm-12\">\n" +
        "                                            <div class=\"ibox float-e-margins\">\n" +
        "                                                <div class=\"ibox-content\">\n" +
        "                                                    <table class=\"table table-bordered\">\n" +
        "                                                        <thead>\n" +
        "                                                        <tr>\n" +
        "                                                            <th>编号</th>\n" +
        "                                                            <th>项目名称</th>\n" +
        "                                                            <th>备注</th>\n" +
        "                                                            <th>#</th>\n" +
        "                                                            <th>新增</th>\n" +
        "                                                            <th>删除</th>\n" +
        "                                                        </tr>\n" +
        "                                                        </thead>\n" +
        "                                                        <tbody class=\"itemId_" + (cateCounter + 1) + "\">\n" +
        "                                                        <tr>\n" +
        "                                                            <td>\n" +
        "                                                                <input name=\"itemNo\" class=\"form-control\" type=\"text\">\n" +
        "                                                            </td>\n" +
        "                                                            <td>\n" +
        "                                                                <input name=\"itemName\" class=\"form-control\" type=\"text\">\n" +
        "                                                            </td>\n" +
        "                                                            <td>\n" +
        "                                                                <input name=\"itemRemark\" class=\"form-control\" type=\"text\">\n" +
        "                                                            </td>\n" +
        "                                                            <td>\n" +
        "                                                                <input name=\"itemIntro\" class=\"form-control\" type=\"text\">\n" +
        "                                                            </td>\n" +
        "                                                            <td>\n" +
        "                                                                <button type=\"button\" class=\"btn btn-primary\" onclick=\"appendRow(" + (cateCounter + 1) + ")\">新增</button>" +
        "                                                            </td>\n" +
        "                                                            <td>\n" +
        "                                                                <button type=\"button\" class=\"btn btn-danger\" onclick=\"removeRow()\">新增</button>" +
        "                                                            </td>\n" +
        "                                                        </tr>\n" +
        "                                                        </tbody>\n" +
        "                                                    </table>\n" +
        "                                                </div>\n" +
        "                                            </div>\n" +
        "                                        </div>\n" +
        "                                    </div>\n" +
        "                                </div>\n" +
        "                            </div>\n" +
        "                        </div>";

    $('.cateId_' + cateCounter).after(cateElement);
    cateCounter = cateCounter + 1;

    //console.log('new cate done. cateCounter:', cateCounter);
}


//新增row
function appendRow(currentTable) {
    var pureRow = "<tr>\n" +
        "  <td>\n" +
        "      <input name=\"itemNo\" class=\"form-control\" type=\"text\">\n" +
        "  </td>\n" +
        "  <td>\n" +
        "      <input name=\"itemName\" class=\"form-control\" type=\"text\">\n" +
        "  </td>\n" +
        "  <td>\n" +
        "      <input name=\"itemRemark\" class=\"form-control\" type=\"text\">\n" +
        "  </td>\n" +
        "  <td>\n" +
        "      <input name=\"itemIntro\" class=\"form-control\" type=\"text\">\n" +
        "  </td>\n" +
        "  <td>\n" +
        "      \n" +
        "  </td>\n" +
        "  <td>\n" +
        "      <button type=\"button\" class=\"btn btn-danger\" onclick=\"removeRow(this)\">删除</button> \n" +
        "  </td>\n" +
        "</tr>";

    $('.itemId_' + currentTable).append(pureRow);
    //$(obj).parents("tbody").append(pureRow);
    //console.log('new row. cateCounter:', cateCounter);
}


function removeRow(obj) {
    $(obj).parents("tr").remove();
    console.log('remove row done.');

}

function countCategoryLines() {
    // console.log('cateCounter: ', cateCounter);
    var categoryTrCount = '';
    for (var i=1; i <= cateCounter; i++) {
        var trCount = $('.itemId_' + i + ' tr').length;
        categoryTrCount = categoryTrCount + trCount + ",";
    }

    console.log('categoryTrCount: ', categoryTrCount);
    $("#categoryTrCount").val(categoryTrCount);
}

